﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Common
{
    public static class EventLogManager
    {
        #region Consts

        private const String LOG_SOURCE = "PresbetonReadDataService";
        private const String LOG_NAME = "PresbetonReadDataService";

        private const Int32 INFO_EVENT_ID = 110;
        private const Int32 ERROR_EVENT_ID = 111;

        #endregion Consts

        #region Private Members

        //private volatile static EventLogManager m_instance;
        //private static readonly object m_syncRoot = new Object();

        #endregion Private Members

        #region Private Methods

        /// <summary>
        /// Check and create Event log source if doesn't exists
        /// </summary>
        private static void CheckEventLogSource()
        {
            if (!EventLog.SourceExists(LOG_SOURCE))
            {
                EventSourceCreationData lEventSourceCreationData = new EventSourceCreationData(LOG_SOURCE, LOG_NAME);
                EventLog.CreateEventSource(lEventSourceCreationData);
            }
        }

        #endregion Private Methods



        public static void LogError(Exception ex)
        {
#if !DEBUG
			CheckEventLogSource();

			EventLog.WriteEntry(LOG_SOURCE,
				String.Format("MachineName:{0}\r\n{1}\r\n{2}", Environment.MachineName, ex.Message, ex.StackTrace), 
				EventLogEntryType.Error, 
				ERROR_EVENT_ID);
#endif
        }

        public static void WriteToLog(String message)
        {
            CheckEventLogSource();
            EventLog.WriteEntry(LOG_SOURCE, message, EventLogEntryType.Information, ERROR_EVENT_ID);
        }
    }
}
